﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyHocPhi.BLL;
using QuanLyHocPhi.Model;
using System.IO;
using System.Diagnostics;
using QuanLyHocPhi.GUI.DuLieu;
using System.Net.Http;
using Newtonsoft.Json;
using System.Threading;

namespace QuanLyHocPhi.GUI.TacNghiep
{
    public partial class FormThuLeHocPhiCaNhan : Form
    {
        #region Variables
        bool lockUpdate = false;
        public SinhVien selectedSinhVien { get; set; }
        bool LaHocPhi = true;
        string fileName = "";
        string fileLuu = string.Format("{0}\\TEMPLATES\\PhieuThu.xml",
               Application.StartupPath);
        string fileIn = string.Format("{0}\\TEMPLATES\\PhieuThu.doc",
               Application.StartupPath);
        bool suDungDuLieuTinChi = false;
        int thoiGianChoTaiDuLieu;
        bool _IsLoadFinished = false;
        #endregion

        public FormThuLeHocPhiCaNhan(bool laHocPhi = true)
        {
            InitializeComponent();
            gridLopHocPhan.AutoGenerateColumns = false;
            this.LaHocPhi = laHocPhi;
            if (LaHocPhi)
            {
                lbLeHocPhi.Text = "Học phí";
                this.Text = lbTieuDe.Text = "Thu học phí theo cá nhân";
                fileName = string.Format("{0}\\TEMPLATES\\MauThuHocPhi.xml",
               Application.StartupPath);
            }
            else
            {
                lbLeHocPhi.Text = "Lệ phí";
                this.Text = lbTieuDe.Text = "Thu lệ phí theo cá nhân";
                fileName = string.Format("{0}\\TEMPLATES\\MauThuLePhi.xml",
               Application.StartupPath);
            }
            #region Cấu hình lưới lịch sử nộp
            colNgayThu.DataPropertyName = "NgayThuStr";
            colNoiDungThu.DataPropertyName = "NoiDungThu";
            colSoPhieu.DataPropertyName = "SoPhieuNew";
            colSoQuyen.DataPropertyName = "SoQuyen";
            colSoTienDaNop.DataPropertyName = "SoTienStr";
            gridLichSuNop.AutoGenerateColumns = false;
            gridLichSuNop.AllowDrop = false;
            gridLichSuNop.AllowUserToAddRows = false;
            gridLichSuNop.AllowUserToDeleteRows = false;
            #endregion

            #region Cấu hình lưới danh sách sinh viên
            gridDSSinhVien.AllowDrop = false;
            gridDSSinhVien.AllowUserToAddRows = false;
            gridDSSinhVien.AllowUserToDeleteRows = false;
            gridDSSinhVien.AutoGenerateColumns = false;
            gridDSSinhVien.ShowFindBar = false;
            colMaSV.DataPropertyName = "MaSinhVien";
            colHoDem.DataPropertyName = "HoDem";
            colTen.DataPropertyName = "Ten";
            #endregion

            LoadNamHoc();
        }

        private void LoadNamHoc()
        {
            lockUpdate = true;
            var ds = BizNamHoc.GetListNamHoc();
            InitComboSuggestion(cmbNamHoc, ds, "TenNamHoc", "TenNamHoc", ds.Select(e => e.TenNamHoc).ToArray());
            LoadHocKy();
        }

        private void LoadHocKy()
        {
            cmbHocKy.DataSource = null;
            if ((cmbNamHoc.SelectedItem as NamHoc) != null)
            {
                var ds = BizHocKy.GetList_HocKy((cmbNamHoc.SelectedItem as NamHoc).TenNamHoc);
                //InitComboSuggestion(cmbHocKy, ds, "TenHocKy", "MaHocKy", ds.Select(e => e.TenHocKy.ToString()).ToArray());
                cmbHocKy.DataSource = ds;
                cmbHocKy.DisplayMember = "TenHocKy";
                cmbHocKy.ValueMember = "MaHocKy";
                if (ds != null && ds.Count > 0)
                {
                    if (bdsDsSinhVien.DataSource != null)
                    {
                        bntNhap.Enabled = true;
                        bntNhapVaIn.Enabled = true;
                    }
                }
                else
                {
                    bntNhap.Enabled = false;
                    bntNhapVaIn.Enabled = false;
                }

            }
            LoadNoiDungHocPhi();
            LoadLopGoc();
        }

        private void LoadNoiDungHocPhi()
        {
            txtNoiDungThu.Text = "";
            if (cmbNamHoc.SelectedIndex != -1 && cmbHocKy.SelectedIndex != -1)
            {
                var hk = cmbHocKy.SelectedItem as HocKy;
                var nh = cmbNamHoc.SelectedItem as NamHoc;
                txtNoiDungThu.Text = string.Format("{2} học kỳ {0} năm học {1}", hk.TenHocKy, nh.TenNamHoc, LaHocPhi ? "Học phí" : "Lệ phí");
            }
           
        }

        private void LoadNamTuyenSinh()
        {
            lockUpdate = true;
            cmbNamTuyenSinh.DataSource = null;
            var ds = BizLoaiHinhDaoTao.GetList_NamTuyenSinh((cmbLoaiHinh.SelectedItem as LoaiHinhDaoTao).MaLoaiHinh);
            InitComboSuggestion(cmbNamTuyenSinh, ds, null, null, ds.ToArray());
            //cmbNamTuyenSinh.DataSource = ds;
            LoadLopGoc();
        }

        private void LoadLopGoc()
        {
            lockUpdate = true;
            cmbLop.DataSource = null;
            var ds = new List<LopGoc>();
            FormProgress frm = new FormProgress();
            frm.DoWork += delegate(object oSender, DoWorkEventArgs oE)
            {
                ds = BizLopGoc.GetListLopGoc();
            };
            #region On complete
            frm.RunWorkerCompleted += delegate(object oSender, RunWorkerCompletedEventArgs oE)
            {
                if (oE.Error != null)
                {
                    string msg = oE.Error.Message;
                    if (oE.Error.InnerException != null)
                        msg += oE.Error.InnerException.Message;
                    DialogUtils.ShowError(msg);
                }
                InitComboSuggestion(cmbLop, ds, "TenLopGoc", "MaLopGoc", ds.Select(e => e.TenLopGoc).ToArray());
                
            };
            #endregion

            frm.StartWorking(this);

            //lockUpdate = false;
            //cmbLop_SelectedIndexChanged(this, new EventArgs());

        }



        

        private void LoadLeHocPhi()
        {
            suDungDuLieuTinChi = false;
            bsLopHocPhan.DataSource = null;
            if (cmbHocKy.SelectedIndex != -1 && bdsDsSinhVien.Current != null)
            {
                if (selectedSinhVien != null)
                {
                    if (LaHocPhi && selectedSinhVien.LopGoc.MaLoaiHinhDaoTao == "CQ")
                    {
                        suDungDuLieuTinChi = true;
                        GetStudentInformationFromCEMS();
                        var frg = new FormProgress();
                        frg.DoWork += (o, e) =>
                        {
                            thoiGianChoTaiDuLieu = 0;
                            while (!_IsLoadFinished)
                            {
                                Thread.Sleep(2000);
                                thoiGianChoTaiDuLieu++;
                                if (thoiGianChoTaiDuLieu > 20)
                                    break;
                            }
                        };
                        frg.RunWorkerCompleted += (o, e) =>
                        {
                        };
                        frg.StartWorking(this);
                    }
                    else
                    {
                        var lhp = BizPhiMacDinh.GetLeHocPhi(selectedSinhVien.MaLopGoc, (cmbHocKy.SelectedItem as HocKy).MaHocKy, LaHocPhi);
                        txtHocPhi.Text = lhp.ToString();
                        txtSoTienNop.Text = lhp.ToString();
                    }
                }
            }
            else
            {
                txtHocPhi.Text = "";
                txtSoTienNop.Text = "";
            }
            gridLopHocPhan.DataSource = bsLopHocPhan;
        }

        private void LoadLichSuNopHP()
        {
            bdsLichSuNop.DataSource = null;
            long tongTienDaNop = 0;
            lbTongTienDaNop.Text = "0 đồng";
            if (bdsDsSinhVien.Current != null && cmbHocKy.SelectedIndex != -1)
            {
                var ds = BizPhieuThu.GetLichSuNopLePhi1HocKy((bdsDsSinhVien.Current as SinhVien).MaSinhVien, (cmbHocKy.SelectedItem as HocKy).MaHocKy, ref tongTienDaNop, LaHocPhi);
                bdsLichSuNop.DataSource = ds;

                lbTongTienDaNop.Text = tongTienDaNop.ToString().FormatCurrency() + " đồng";
            }
            gridLichSuNop.DataSource = bdsLichSuNop;
        }

        private void LoadDanhSachSinhVien()
        {
            lockUpdate = true;
            bdsDsSinhVien.DataSource = null;
            gridDSSinhVien.DataSource = bdsDsSinhVien;
            if (cmbLop.SelectedIndex != -1)
            {
                LopGoc lopGoc = null;
                if (cmbLop.SelectedItem != null)
                    lopGoc = cmbLop.SelectedItem as LopGoc;
                if (lopGoc == null)
                    return;
                HocKy hocKy = null;
                if (cmbHocKy.SelectedItem != null)
                    hocKy = cmbHocKy.SelectedItem as HocKy;
                if (hocKy == null)
                    return;

                var ds = BizSinhVien.GetDanhSachSinhVien(lopGoc.MaLopGoc, hocKy.MaHocKy, LaHocPhi, txtKeyword.Text.Trim());
                bdsDsSinhVien.DataSource = ds;
                gridDSSinhVien.DataSource = bdsDsSinhVien;
            }
            LoadThongTinSinhVien();
        }

        private void LoadThongTinSinhVien()
        {
            if (bdsDsSinhVien.DataSource != null && (bdsDsSinhVien.Current as SinhVien) != null)
            {
                selectedSinhVien = bdsDsSinhVien.Current as SinhVien;
                if (selectedSinhVien != null)
                {
                    selectedSinhVien = BizSinhVien.GetSinhVienByMSV(selectedSinhVien.MaSinhVien);
                    txtMaSinhVien.Text = selectedSinhVien.MaSinhVien;
                    txtHoTen.Text = selectedSinhVien.HoDem + " " + selectedSinhVien.Ten;
                    txtNgaySinh.Text = string.Format("{0:dd/MM/YYYY}", selectedSinhVien.NgaySinh);
                }
            }
            else
            {
                selectedSinhVien = null;
                txtMaSinhVien.Text = "";
                txtHoTen.Text = "";
                txtNgaySinh.Text = "";
            }
        }

        public async void GetStudentInformationFromCEMS()
        {
            _IsLoadFinished = false;
            List<HocPhiTinChi> list = null;

            using (var client = new HttpClient())
            {
                var data = new StringContent("{maSinhVien:12345,hocKy:2014-2015.2}", Encoding.Unicode, "application/json");
                using (var respone = await client.GetAsync("http://localhost:37769/api/Values?maSinhVien=12345&hocKy=2014-2015.2"))
                {
                    if (respone.IsSuccessStatusCode)
                    {
                        var result = await respone.Content.ReadAsStringAsync();
                        var rs = JsonConvert.DeserializeObject<Student>(result);
                        if (rs != null)
                        {
                            if (rs.ChiTiet != null && rs.ChiTiet.Count > 0)
                            {
                                list = new List<HocPhiTinChi>();
                                foreach (var ct in rs.ChiTiet)
                                {
                                    list.Add(new HocPhiTinChi
                                    {
                                        _IsSelected = true,
                                        SoTien = ct.SoTien,
                                        TenLopHocPhan = ct.TenLopHocPhan,
                                        MaLopHocPhan = ct.MaLopHocPhan,
                                        MaHocTap = ct.MaHocTap
                                    });
                                }

                            }
                        }
                    }
                }
            }
            if (list != null && list.Count > 0)
            {
                txtHocPhi.Text = list.Sum(t => t.SoTien).ToString();
                txtSoTienNop.Text = list.Sum(t => t.SoTien).ToString();
            }
            else
            {
                txtHocPhi.Text = "";
                txtSoTienNop.Text = "";
            }
            bsLopHocPhan.DataSource = list;
            gridLopHocPhan.DataSource = bsLopHocPhan;
            _IsLoadFinished = true;
        }

        public async void PostDataToCEMS()
        {
            using (var client = new HttpClient())
            {
                var listHPTC = bsLopHocPhan.DataSource as List<HocPhiTinChi>;
                if (suDungDuLieuTinChi && listHPTC != null && listHPTC.Count > 0)
                {
                    PhieuThuCEMS phieuThuCEMS = new PhieuThuCEMS();
                    phieuThuCEMS.MaSinhVien = selectedSinhVien.MaSinhVien;
                    phieuThuCEMS.NgayThu = DateTime.Now.ToShortDateString();
                    phieuThuCEMS.HocKy = string.Format("{0}.{1}", cmbNamHoc.SelectedText, cmbHocKy.SelectedText);
                    List<LopHocPhan> listLHP = new List<LopHocPhan>();
                    foreach (var item in listHPTC)
                    {
                        listLHP.Add(new LopHocPhan
                        {
                            MaHocTap = item.MaHocTap,
                            MaLopHocPhan = item.MaLopHocPhan,
                            TenLopHocPhan = item.TenLopHocPhan,
                            SoTien = item.SoTien
                        });
                    }
                    phieuThuCEMS.ChiTiet = listLHP;
                    var oPhieuThuCEMS = JsonConvert.SerializeObject(phieuThuCEMS);
                    var sPhieuThuCEMS = new StringContent(oPhieuThuCEMS, Encoding.UTF8, "application/json");
                    var result = await client.PostAsync("http://localhost:37769/api/Values", sPhieuThuCEMS);
                    if (result.Content.ReadAsStringAsync().Result == "true")
                        MessageBox.Show("OK");
                }
            }
        }

        private void cmbNamHoc_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!lockUpdate)
            {
                LoadHocKy();
            }
        }

        private void cmbHocKy_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!lockUpdate)
            {
                LoadLeHocPhi();
                LoadNoiDungHocPhi();
                LoadLichSuNopHP();
            }
        }

        private void bntNhap_Click(object sender, EventArgs e)
        {
            //PostDataToCEMS();
            NhapPhieuThuVaoCSDL();
        }

        private void bntNhapVaIn_Click(object sender, EventArgs e)
        {
            NhapPhieuThuVaoCSDL(true);
        }

        void NhapPhieuThuVaoCSDL(bool inPhieuThu = false)
        {
            #region Lấy thông tin học kỳ
            if (cmbHocKy.SelectedIndex == -1)
                return;
            var hocKy = cmbHocKy.SelectedItem as HocKy;
            #endregion

            #region Lấy thông tin học phí
            int sHocPhi = 0;
            int temp = 0;
            var soTien = txtHocPhi.Text.Replace(".", "").Replace(",", "");
            if (int.TryParse(soTien, out temp))
                sHocPhi = int.Parse(soTien);
            else
            {
                DialogUtils.ShowError("Nhập sai học phí");
                return;
            }
            int sSoTienNop = 0;
            soTien = txtSoTienNop.Text.Replace(".", "").Replace(",", "");
            if (int.TryParse(soTien, out temp))
                sSoTienNop = int.Parse(soTien);
            else
            {
                DialogUtils.ShowError("Nhập sai số tiền cần nộp");
                return;
            }
            #endregion

            #region Lấy thông tin về số biên lai
            List<string> cauHinhBienLai = BizCauHinh.GetCauHinh(LaHocPhi);
            var quyenSo = "1";
            var soPhieu = "1";
            var tongSoPhieu = "50";
            var soPhieuLonNhat = "2000";
            if (cauHinhBienLai != null && cauHinhBienLai.Count >= 3)
            {
                quyenSo = cauHinhBienLai[0];
                soPhieu = cauHinhBienLai[1];
                tongSoPhieu = cauHinhBienLai[2];
                soPhieuLonNhat = cauHinhBienLai[3];
            }
            else
            {
                DialogUtils.ShowError("Thông tin cấu hình biên lai không hợp lệ");
                return;
            }
            #endregion

            //Biên chế sinh viên vào một một học kỳ
            var bienChe = BizBienCheSinhVienVaoHocKy.BienCheSinhVien(selectedSinhVien.MaSinhVien, hocKy.MaHocKy, sHocPhi, LaHocPhi);
            //Tạo mới một phiếu thu
            var phieuThu = new PhieuThu()
            {
                MaPhieuThu = Guid.NewGuid().ToString(),
                SoQuyen = "",
                SoPhieu = "",
                NoiDungThu = txtNoiDungThu.Text,
                NgayThu = dateNgayNop.Value.Date,
                MaBienChe = bienChe.MaBienChe,
                MaNguoiThu = Program.NguoiSuDung.TenDangNhap,
                SoTien = sSoTienNop,
                DaIn = false
            };
            if (LaHocPhi)
                phieuThu.LaHocPhi = true;
            else
                phieuThu.LaHocPhi = false;

            if (inPhieuThu)
            {
                phieuThu.SoPhieu = soPhieu;
                phieuThu.SoQuyen = quyenSo;
            }

            #region Tạo mới phiếu thu, cập nhật số biên lai và in phiếu thu
            if (BizPhieuThu.CreatePhieuThu(phieuThu))
            {
                if (inPhieuThu)
                {
                    InPhieuThu(phieuThu);
                    //Tăng số phiếu và số quyển
                    BizCauHinh.IncrementCauHinh(ref quyenSo, ref soPhieu, tongSoPhieu, soPhieuLonNhat);
                    //Cập nhật lại số biên lai
                    BizCauHinh.UpdateCauHinh(quyenSo, soPhieu, tongSoPhieu, soPhieuLonNhat, LaHocPhi);
                }
            }
            #endregion

            DialogUtils.ShowMessage("Đã lập phiếu thu cho sinh viên " + selectedSinhVien.HoDem + " " + selectedSinhVien.Ten + " thành công");
            LoadLichSuNopHP();
        }

        private void InPhieuThu(PhieuThu phieuThu)
        {
            try
            {
                if (!File.Exists(fileName))
                {
                    DialogUtils.ShowMessage("File mẫu phiếu thu không tồn tại. Xin vui lòng kiểm tra lại hoặc liên hệ với người quản trị");
                    return;
                }
                if (File.Exists(fileLuu))
                    File.Delete(fileLuu);
                if (File.Exists(fileIn))
                    File.Delete(fileIn);
                var pt = BizPhieuThu.GetPhieuThuFull(phieuThu.MaPhieuThu);

                String fileContents = System.IO.File.ReadAllText(fileName);
                fileContents = fileContents.Replace("{ngay}", pt.NgayThu.Day.ToString());
                fileContents = fileContents.Replace("{thang}", pt.NgayThu.Month.ToString());
                fileContents = fileContents.Replace("{nam}", pt.NgayThu.Year.ToString());
                fileContents = fileContents.Replace("{quyenSo}", pt.SoQuyen);
                fileContents = fileContents.Replace("{soPhieu}", pt.SoPhieu);
                fileContents = fileContents.Replace("{tenSinhVien}", pt.BienCheSinhVienVaoHocKy.SinhVien.HoDem + " " + pt.BienCheSinhVienVaoHocKy.SinhVien.Ten);
                fileContents = fileContents.Replace("{diaChi}", pt.BienCheSinhVienVaoHocKy.SinhVien.LopGoc.TenLopGoc);
                fileContents = fileContents.Replace("{noiDung}", pt.NoiDungThu);
                fileContents = fileContents.Replace("{soTien}", pt.SoTien.ToString().FormatCurrency());
                fileContents = fileContents.Replace("{soTienBangChu}", pt.SoTien.ToString().ConvertNum2Text());
                fileContents = fileContents.Replace("{nguoiThu}", pt.NguoiSuDung.HoTen);
                fileContents = fileContents.Replace("{banSao}", "");
                System.IO.File.WriteAllText(fileLuu, fileContents);
                File.Move(fileLuu, fileIn);
                ProcessStartInfo info = new ProcessStartInfo(fileIn);
                info.Verb = "Print";
                info.CreateNoWindow = true;
                info.WindowStyle = ProcessWindowStyle.Hidden;
                Process.Start(info);
                phieuThu.DaIn = true;
                BizPhieuThu.Update(phieuThu);
            }
            catch (Exception ex)
            {
                DialogUtils.ShowError("Lỗi: " + ex.ToString());
            }
        }

        private void bdsLichSuNop_DataSourceChanged(object sender, EventArgs e)
        {

        }

        private void gridLichSuNop_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void InitComboSuggestion(ComboBox cmb, object datasource, string displayMember, string valueMember, string[] rangeDisplay)
        {
            cmb.AutoCompleteSource = AutoCompleteSource.CustomSource;
            cmb.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            cmb.DataSource = datasource;
            if (displayMember != null)
                cmb.DisplayMember = displayMember;
            if (valueMember != null)
                cmb.ValueMember = valueMember;
            // Xác định AutoCompleteCustomeSource
            var autoCompletSet = new AutoCompleteStringCollection();
            autoCompletSet.AddRange(rangeDisplay);
            cmb.AutoCompleteCustomSource = autoCompletSet;

            // Các tinh chỉnh nâng cao
            cmb.Leave += (oSender, oE) =>
            {
                var cbx = oSender as ComboBox;
                if (cbx == null)
                    return;
                cbx.BackColor = cbx.SelectedItem == null ? Color.LightPink : SystemColors.Window;
            };

            cmb.KeyDown += (oSender, oE) =>
            {
                if (oE.KeyCode != Keys.Enter)
                    return;
                var cbx = oSender as ComboBox;
                if (cbx == null)
                    return;
                cbx.BackColor = cbx.SelectedItem == null ? Color.LightPink : SystemColors.Window;
            };
        }

        private void cmbLop_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!lockUpdate)
            {
                LopGoc lop = cmbLop.SelectedItem as LopGoc;
                if (lop != null)
                {
                    txtLoaiHinh.Text = lop.LoaiHinhDaoTao.TenLoaiHinh;
                    txtNTS.Text = lop.NamTuyenSinh;
                    LoadDanhSachSinhVien();
                    //LoadLeHocPhi();
                }
            }
        }

        private void cmbLoaiHinh_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!lockUpdate)
            {
                LoadNamTuyenSinh();
            }
        }

        private void cmbNamTuyenSinh_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!lockUpdate)
            {
                LoadLopGoc();
            }
        }

        private void bdsDsSinhVien_CurrentChanged(object sender, EventArgs e)
        {
            if (!lockUpdate)
            {
                LoadThongTinSinhVien();
                LoadLeHocPhi();
                LoadLichSuNopHP();
            }
        }

        private void bdsDsSinhVien_DataSourceChanged(object sender, EventArgs e)
        {
            if (bdsDsSinhVien.DataSource != null && cmbHocKy.DataSource != null)
            {
                bntNhap.Enabled = true;
                bntNhapVaIn.Enabled = true;
            }
            else
            {
                bntNhap.Enabled = false;
                bntNhapVaIn.Enabled = false;
            }
            lblDanhSachSinhVien_ThongKe.Text = string.Format("{0} sinh viên", bdsDsSinhVien.Count);
        }

        private void cmbHocKy_DataSourceChanged(object sender, EventArgs e)
        {
            if (bdsDsSinhVien.DataSource != null && cmbHocKy.DataSource != null)
            {
                bntNhap.Enabled = true;
                bntNhapVaIn.Enabled = true;
            }
            else
            {
                bntNhap.Enabled = false;
                bntNhapVaIn.Enabled = false;
            }
        }

        private void txtKeyword_TextChanged(object sender, EventArgs e)
        {
            LoadDanhSachSinhVien();
        }

        private void txtSoTienNop_TextChanged(object sender, EventArgs e)
        {
            int temp = 0;
            var soTienNop = txtSoTienNop.Text.Replace(".", "").Replace(",", "");
            if (int.TryParse(soTienNop, out temp))
            {

                lbTienBangChu.Text = int.Parse(soTienNop).ToString().ConvertNum2Text();
                txtSoTienNop.Text = soTienNop.FormatCurrency();
                txtSoTienNop.Select(txtSoTienNop.Text.Length, 0);
            }
        }

        private void txtHocPhi_TextChanged(object sender, EventArgs e)
        {
            int temp = 0;
            var hocPhi = txtHocPhi.Text.Replace(".", "").Replace(",", "");
            if (int.TryParse(hocPhi, out temp))
            {
                txtHocPhi.Text = hocPhi.FormatCurrency();
                txtHocPhi.Select(txtHocPhi.Text.Length, 0);
            }
        }

        private void gridLopHocPhan_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == colCheck.Index)
            {
                gridLopHocPhan.CommitEdit(DataGridViewDataErrorContexts.Commit);

                var listHPTC = bsLopHocPhan.DataSource as List<HocPhiTinChi>;
                double tongTien = 0;
                if (listHPTC != null && listHPTC.Count > 0)
                {
                    tongTien = listHPTC.Where(t => t._IsSelected == true).Sum(t => t.SoTien);
                }
                txtSoTienNop.Text = tongTien.ToString();
            }
        }
    }
    public class Student
    {
        public string MaSinhVien { get; set; }
        public string HoTen { get; set; }
        public string NgaySinh { get; set; }
        public List<LopHocPhan> ChiTiet { get; set; }
    }
    public class LopHocPhan
    {
        public string MaHocTap { get; set; }
        public string MaLopHocPhan { get; set; }
        public string TenLopHocPhan { get; set; }
        public double SoTien { get; set; }
    }
    public class PhieuThuCEMS
    {
        public string MaSinhVien { get; set; }
        public string HocKy { get; set; }
        public string NgayThu { get; set; }
        public List<LopHocPhan> ChiTiet { get; set; }
    }
}
